library(ggplot2)
library(readxl)
library(dplyr)
library(stringr)

data <- read_excel("politician_tweet_count.xlsx")
floyd_map <- data[!is.na(data$geoid),]
floyd_map$geoid <- str_pad(floyd_map$geoid, 4, pad="0")
floyd_map <- floyd_map[,-5]

library(USAboundaries)
# ?us_congressional to see other options
cong.map <- us_congressional(resolution = "high")
ggplot(cong.map) +
  geom_sf()
cong.map <- filter(cong.map, state_name %in% state.name & !(state_name %in% c("Hawaii", "Alaska")))
ggplot(cong.map) +
  geom_sf()
floyd.map <- left_join(cong.map, floyd_map, by = "geoid")
names(floyd.map)[4] <- "fips"
floyd.map$tweet_quart <- NA
floyd.map$tweet_quart[which(floyd.map$no_tweets %in% c(0,1))] <- "0-1"
floyd.map$tweet_quart[which(floyd.map$no_tweets %in% c(2,3))] <- "2-3"
floyd.map$tweet_quart[which(floyd.map$no_tweets %in% c(4,5,6,7))] <- "4-7"
floyd.map$tweet_quart[which(floyd.map$no_tweets > 7)] <- "8 or more"
floyd.map$tweet_quart[which(is.na(floyd.map$no_tweets))] <- "NA"

p1 <- floyd.map %>% 
  ggplot(aes(fill = tweet_quart)) +
  geom_sf(color="#444444", linewidth=.5, alpha=.7) +
  geom_sf(fill=NA, color="black", linewidth = 0.75, data = states) +
  scale_fill_manual(values=c("white","#BDD7E7","#6BAED6","#08519C","#D3D3D3"))+
  labs(fill="Number of Floyd Tweets") +
  theme_classic() +
  theme(title = element_text(size=15),
        axis.text = element_blank(),
        axis.line = element_blank(),
        axis.ticks = element_blank(),
        legend.position = c(0.95,0.3),
        legend.title = element_text(size = 19.5),
        legend.text = element_text(size = 17)) +
  guides(fill = guide_legend(title = "Number of\nFloyd Tweets"))
ggsave(filename="Figure9.png", width=15, height=9, plot=p1, dpi=600)